<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
    </div>
    <div class="search-list_content">
      <van-list
        v-model="loading"
        :finished="done"
        :immediate-check="false"
        offset="50"
        @load="pullUpLoadMoreFn"
      >
        <ArticleItem
          v-for="item in resultList"
          :key="item.art_id"
          :obj="item"
          :isShowCross="false"
      /></van-list>
    </div>
  </div>
</template>

<script>
import { searchResultListAPI } from '@/api/search'
// 页码默认是1
let page = 1
export default {
  name: 'SearchResult',
  data () {
    return {
      resultList: [], // 搜索结果列表
      loading: false,
      done: false
    }
  },
  async activated () {
    this.getsearchResultList()
  },
  deactivated () {
    page = 1
    this.resultList = []
  },
  computed: {
    txt () {
      return this.$route.params.keywords
    }
  },

  methods: {
    // 获取搜索结果列表
    async getsearchResultList () {
      const res = await searchResultListAPI({
        keyword: this.txt, // 拿到关键词, keywords来源于router/index.js你定义的动态路由参数名
        page: page++
      })
      const { results } = res.data
      if (results.length < 10) {
        this.done = true
        return
      }
      this.resultList = [...this.resultList, ...results]
      this.loading = false
    },
    // 上拉加载更多
    pullUpLoadMoreFn () {
      if (this.resultList.length > 0) {
        this.getsearchResultList()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
